package org.jeecg.modules.system.parse.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.constant.TemplateConstant;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.parse.IFileParse;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.util.SecurityUtil;
import org.jeecg.modules.villageCadreAssist.entity.VcaFamily;
import org.jeecg.modules.villageCadreAssist.entity.VcaResident;
import org.jeecg.modules.villageCadreAssist.service.IVcaFamilyService;
import org.jeecg.modules.villageCadreAssist.service.IVcaResidentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

@Component
public class DocxFileParse implements IFileParse {

    @Autowired
    private IVcaResidentService vcaResidentService;
    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private IVcaFamilyService vcaFamilyService;
    @Autowired
    private ISysUserService sysUserService;

    private static String SUFFIX = ".docx";

    @Override
    public boolean support(String url) {
        return url.endsWith(SUFFIX);
    }

    @Override
    public void parse(String urlStr,Map<String,Object> dataMap, OutputStream outputStream) {
        ZipOutputStream zipout= new ZipOutputStream(outputStream);
        BufferedInputStream bs = null;
        HttpURLConnection conn=null;
        try{
            URL url = new URL(urlStr);
            conn = (HttpURLConnection) url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            //得到输入流
            InputStream inputStream = conn.getInputStream();
            ZipInputStream zin = new ZipInputStream(inputStream, Charset.forName("utf-8"));
            bs = new BufferedInputStream(zin);
            ZipEntry next;
            byte[] buffer;
            while ((next= zin.getNextEntry())!=null) {
                zipout.putNextEntry(new ZipEntry(next.getName()));
                buffer = new byte[(int) next.getSize()];
                bs.read(buffer, 0, (int) next.getSize());
                if ("word/document.xml".equals(next.getName())) {//模板文件
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
                    InputStreamReader reader = new InputStreamReader(byteArrayInputStream);
                    Template template =  new Template(null,reader,new Configuration(Configuration.VERSION_2_3_28));
                    template.process(dataMap, new OutputStreamWriter(zipout));
                }else{
                    zipout.write(buffer);
                }
            }
            zipout.finish();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if(conn != null){
                conn.disconnect();
            }
        }
    }
}
